<template>
  <view class="commonLim-list">
    <!-- 表单内容 -->
    <view class="form-content flexandauto">
      <view class="form-item">
        <view>
          <text class="label" style="color: red">*</text>
          <text class="label">分轮编号:</text>
        </view>
        <view class="flexCenter" style="flex: 1">
          <input
            class="iptClass textright"
            placeholder="输入或扫分轮二维码"
            v-model.trim="profile.trackingNo"
            style="width: 100%"
            @blur="handleblur1"
          />
          <image
            src="../../static/tabs/qr.png"
            @click="handlecan1"
            class="qrClass mgl10px"
          />
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">物料编码:</text>
        </view>
        <view>
          <text v-if="profile.materialCode" class="iptClass">{{
            profile.materialCode
          }}</text>
          <text class="dianwo" v-else>根据分轮编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">材质:</text>
        </view>
        <view>
          <text v-if="profile.materialTypeName" class="iptClass">{{
            profile.materialTypeName
          }}</text>
          <text class="dianwo" v-else>根据分轮编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">规格型号:</text>
        </view>
        <view>
          <text v-if="profile.specificationName" class="iptClass">{{
            profile.specificationName
          }}</text>
          <text class="dianwo" v-else>根据分轮编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">炉号:</text>
        </view>
        <view>
          <text v-if="profile.heatNo" class="iptClass">{{
            profile.heatNo
          }}</text>
          <text class="dianwo" v-else>根据分轮编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">分轮重量:</text>
        </view>
        <view>
          <text v-if="profile.plannedWeight" class="iptClass">{{
            profile.plannedWeight
          }}</text>
          <text class="dianwo" v-else>根据分轮编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">预拆分数:</text>
        </view>
        <view>
          <text v-if="profile.plannedSplitCount" class="iptClass">{{
            profile.plannedSplitCount
          }}</text>
          <text class="dianwo" v-else>根据分轮编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label" style="color: red">*</text>
          <text class="label">重量:</text>
        </view>
        <view class="flexCenter" style="flex: 1">
          <input
            class="iptClass textright"
            placeholder="请输入重量"
            v-model="weight"
            type="number"
            style="width: 100%"
          />
          <text>kg</text>
        </view>
      </view>
    </view>
    <!-- 提交按钮 -->
    <view class="dengjiClass">
      <button @click="onSubmit" class="commonClass">保存</button>
    </view>
    <view class="pageClass">
      <up-popup :show="showReject" mode="bottom" closeOnClickOverlay>
        <view class="btn-group" style="justify-content: flex-end">
          <text class="confirm" @click="handleSure">确定</text>
        </view>
        <view class="popup-content" style="margin: 60rpx">
          <view style="margin-left: 40rpx"
            >未设置预拆分数，请先设置预拆分数。</view
          >
          <view class="fenlun">
            <view class="titleClass">分轮预拆分数：</view>
            <up-input
              placeholder="请输入分轮分数"
              v-model="plannedSplitCount"
              type="number"
            ></up-input>
          </view>
        </view>
      </up-popup>
    </view>
  </view>
</template>

<script setup>
import { roundsSplit, saomarequest1, plannedFnSplitCount } from "./api.js";
import { onCan } from "@/utils/index.js";
const plannedSplitCount = ref("");
const showReject = ref(false);
const profile = ref({});
const weight = ref("");
// 点击提交表单
const onSubmit = async () => {
  try {
    if (!profile.value.trackingNo) {
      return uni.showToast({
        icon: "none",
        title: "输入或扫分轮二维码",
      });
    }
    if (!+weight.value) {
      return uni.showToast({
        icon: "none",
        title: "重量不能为空或0",
      });
    }
    uni.showLoading({
      title: "加载中",
    });
    await roundsSplit({
      roundId: profile.value.id,
      weight: +weight.value,
    });
    uni.showToast({ icon: "success", title: "保存成功" });
    uni.navigateBack({ delta: 1 });
  } finally {
    uni.hideLoading();
  }
};
const fengzhuang1 = async (value) => {
  profile.value.trackingNo = value;
  const res = await saomarequest1(value);
  if (res) {
    profile.value = res;
    profile.value.trackingNo = value;
    if (!profile.value.plannedSplitCount) {
      showReject.value = true;
    }
  }
};

const handleblur1 = async (result) => {
  const value = result.detail.value;
  if (value) {
    fengzhuang1(value);
  }
};

const handlecan1 = () => {
  onCan(fengzhuang1);
};
const handleSure = async () => {
  if (!+plannedSplitCount.value) {
    return uni.showToast({
      icon: "none",
      title: "分轮数量不能为空或0",
    });
  }
  try {
    uni.showLoading({
      title: "加载中",
    });
    await plannedFnSplitCount(profile.value.id, +plannedSplitCount.value);
    uni.showToast({ icon: "success", title: "设置预拆分数成功" });
    profile.value.plannedSplitCount = +plannedSplitCount.value;
    showReject.value = false;
  } finally {
    uni.hideLoading();
  }
};
onLoad(() => {});
</script>

<style lang="scss" scoped>
</style>
